package com.jhf.skypac_pda;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.jhf.skypac_pda.EventMessage.MessageEvent;
import com.jhf.skypac_pda.base.BaseActivity;
import com.jhf.skypac_pda.base.BaseFragmentPagerAdapter;
import com.jhf.skypac_pda.fragment.ExceptionDoneFragment;
import com.jhf.skypac_pda.fragment.ExceptionTodoFragment;
import com.jhf.skypac_pda.view.ControlScrollViewPager;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ExceptionStockInActivity extends BaseActivity {
    @BindView(R.id.tbl_recruit)
    TabLayout tblOrder;
    @BindView(R.id.vp_content)
    ControlScrollViewPager viewPager;

    private BaseFragmentPagerAdapter mAdapter;
    private Integer[] titlesRes = new Integer[]{R.string.order_uundelivered, R.string.order_undelivered};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_common_tab);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        List<String> titles = new ArrayList<>();
        for (int i = 0; i < titlesRes.length; i++) {
            titles.add(getResources().getString(titlesRes[i]));
        }

        List<Fragment> fragments = new ArrayList<>();

        fragments.add(ExceptionTodoFragment.newInstance("3", ""));
        fragments.add(ExceptionDoneFragment.newInstance("4", ""));

        mAdapter = new BaseFragmentPagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(fragments.size() - 1);
        viewPager.setScanScroll(false);
        tblOrder.setupWithViewPager(viewPager);
        tblOrder.setTabTextColors(getResources().getColor(R.color.title_text_num), getResources().getColor(R.color.contents_text));
    }

    @OnClick(R.id.tv_back)
    public void onViewClicked() {
        finish();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onShutDown(MessageEvent eventMsg) {
        String msg = eventMsg.getMessage();
        if (eventMsg.getType() == 8) {
            // activity关闭，从栈中移除
            //appManager.finishActivity(this);
            if (ExceptionStockInActivity.this.getWindow().getDecorView().getVisibility() == View.VISIBLE) {
                Toast.makeText(this, "登录已过期", Toast.LENGTH_SHORT).show();
                Log.i("登录过期走到这里=========", "FeedingDetailActivity");
                Intent intent = new Intent(ExceptionStockInActivity.this, LoginActivity.class);
                intent.putExtra("isUpdate", true);
                startActivity(intent);
            }
        }
    }
}
